package com.ms.education.course.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ms.education.R;
import com.ms.education.course.entity.Course;
import com.ms.education.course.entity.PushClass;
import com.ms.education.course.entity.PushCourse;
import com.ms.education.course.entity.PushSchedule;
import com.ms.education.course.entity.vo.PageClassVo;
import com.ms.education.course.entity.vo.PageCourseVo;
import com.ms.education.course.service.PushClassService;
import com.ms.education.course.service.PushCourseService;
import com.ms.education.course.service.PushScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 * 推送计划 前端控制器
 * </p>
 *
 * @author ms
 * @since 2023-03-06
 */
@RestController
@RequestMapping("/course/push-schedule")
@Api(description = "推送计划")
public class PushScheduleController {

    @Autowired
    private PushScheduleService pushScheduleService;

    @Autowired
    private PushCourseService pushCourseService;

    @Autowired
    private PushClassService pushClassService;

    @PostMapping("pagePush/{current}/{limit}")
    public R pagePush(@PathVariable("current") long current,
                      @PathVariable("limit") long limit,
                      @RequestBody PushSchedule pushSchedule) {
        Page<PushSchedule> page = pushScheduleService.pagePush(current, limit, pushSchedule);
        return R.ok().data("rows", page.getTotal()).data("pushes", page.getRecords());
    }

    // TODO  加入缓存
    @Cacheable("TodaySchedulePush")
    @ApiOperation("查询今日推送计划")
    @GetMapping("index")
    public R index() {
        Map<String, Object> res = pushScheduleService.index();
        return R.ok().data(res);
    }


    @ApiOperation("查询计划详情")
    @GetMapping("getPushById/{id}")
    public R getPushById(@PathVariable("id") Integer id,
                         @RequestParam(defaultValue = "false") boolean isContainCourse,
                         @RequestParam(defaultValue = "false") boolean isContainClass) {
        Map<String, Object> res = pushScheduleService.getPushById(id, isContainCourse, isContainClass);
        return R.ok().data(res);
    }

    @ApiOperation("新增推送计划")
    @PostMapping("saveOrUpdatePush")
    public R saveOrUpdatePush(@RequestBody PushSchedule pushSchedule) {
        pushScheduleService.saveOrUpdate(pushSchedule);
        return R.ok();
    }

    @ApiOperation("删除推送计划")
    @PostMapping("deletePush")
    public R deletePush(@RequestBody Integer[] ids) {
        pushScheduleService.deletePush(ids);
        return R.ok();
    }

    @ApiOperation("为计划添加课程")
    @PostMapping("addCourseToPush")
    public R addCourseToPush(@RequestBody PushCourse[] pushCourses) {
        pushCourseService.saveBatch(Arrays.asList(pushCourses));
        return R.ok();
    }

    @ApiOperation("为计划移除课程")
    @DeleteMapping("deleteCoursePush/{courseId}/{pushId}")
    public R deleteCoursePush(@PathVariable("courseId") Integer courseId,
                              @PathVariable("pushId") Integer pushId) {
        pushCourseService.deleteCoursePush(courseId, pushId);
        return R.ok();
    }

    @ApiOperation("为计划添加班级")
    @PostMapping("addClassToPush")
    public R addClassToPush(@RequestBody PushClass[] pushClasses) {
        pushClassService.saveBatch(Arrays.asList(pushClasses));
        return R.ok();
    }

    @ApiOperation("为计划移除班级")
    @DeleteMapping("deleteClassPush/{classId}/{pushId}")
    public R deleteClassPush(@PathVariable("classId") Integer classId,
                             @PathVariable("pushId") Integer pushId) {
        pushClassService.deleteClassPush(classId, pushId);
        return R.ok();
    }


    /************************计划查询：班级，课程**********************/
    @ApiOperation("搜索不在计划内的课程")
    @PostMapping("getCourseNotInPush/{current}/{limit}/{pushId}")
    public R getCourseNotInPush(@PathVariable("current") long current,
                                @PathVariable("limit") long limit,
                                @PathVariable("pushId") Integer pushId,
                                @RequestBody PageCourseVo pageCourseVo) {
        Map<String, Object> res = pushCourseService.getCourseNotInPush(current, limit, pushId, pageCourseVo);
        return R.ok().data(res);
    }

    @ApiOperation("查询不在计划内的班级")
    @PostMapping("getClassNotInPush/{current}/{limit}/{pushId}")
    public R getClassNotInPush(@PathVariable("current") long current,
                               @PathVariable("limit") long limit,
                               @PathVariable("pushId") Integer pushId,
                               @RequestBody PageClassVo pageClassVo) {
        Map<String, Object> res = pushClassService.getClassNotInPush(current, limit, pushId, pageClassVo);
        return R.ok().data(res);
    }


}
